﻿package com.project.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.project.service.DataManage;
import com.project.util.ProjectManagementToProjectFee;
import com.project.mapper.*;
import com.project.entity.*;

public class DataManageService implements DataManage {
	private ProjectManagementMapper projectManagementMapper;
	private UnitInfoMapper unitInfoMapper;
	private PersonInfoMapper personInfoMapper;
	private GeneralManagerMapper generalManagerMapper;
	private ProjectFeeMapper projectFeeMapper;
	private ProjectCheckRecordMapper projectCheckRecordMapper;
	private ProjectSuperVisoryManagementMapper projectSuperVisoryManagementMapper;
	private UserMapper userMapper;
	private ActualResultsMapper actualResultsMapper;
	private ProjectDistributeLogMapper projectDistributeLogMapper;
	private ProjectManagementExample projectmanagementExample;

	
	public ProjectDistributeLogMapper getProjectDistributeLogMapper() {
		return projectDistributeLogMapper;
	}

	public void setProjectDistributeLogMapper(
			ProjectDistributeLogMapper projectDistributeLogMapper) {
		this.projectDistributeLogMapper = projectDistributeLogMapper;
	}

	public ActualResultsMapper getActualResultsMapper() {
		return actualResultsMapper;
	}

	public void setActualResultsMapper(ActualResultsMapper actualResultsMapper) {
		this.actualResultsMapper = actualResultsMapper;
	}

	public UserMapper getUserMapper() {
		return userMapper;
	}

	public void setUserMapper(UserMapper userMapper) {
		this.userMapper = userMapper;
	}

	public ProjectSuperVisoryManagementMapper getProjectSuperVisoryManagementMapper() {
		return projectSuperVisoryManagementMapper;
	}

	public void setProjectSuperVisoryManagementMapper(
			ProjectSuperVisoryManagementMapper projectSuperVisoryManagementMapper) {
		this.projectSuperVisoryManagementMapper = projectSuperVisoryManagementMapper;
	}

	public ProjectCheckRecordMapper getProjectCheckRecordMapper() {
		return projectCheckRecordMapper;
	}

	public void setProjectCheckRecordMapper(
			ProjectCheckRecordMapper projectCheckRecordMapper) {
		this.projectCheckRecordMapper = projectCheckRecordMapper;
	}

	public ProjectFeeMapper getProjectFeeMapper() {
		return projectFeeMapper;
	}

	public void setProjectFeeMapper(ProjectFeeMapper projectFeeMapper) {
		this.projectFeeMapper = projectFeeMapper;
	}

	public GeneralManagerMapper getGeneralManagerMapper() {
		return generalManagerMapper;
	}

	public void setGeneralManagerMapper(GeneralManagerMapper generalManagerMapper) {
		this.generalManagerMapper = generalManagerMapper;
	}
	
	public UnitInfoMapper getUnitInfoMapper() {
		return unitInfoMapper;
	}

	public void setUnitInfoMapper(UnitInfoMapper unitInfoMapper) {
		this.unitInfoMapper = unitInfoMapper;
	}

	public PersonInfoMapper getPersonInfoMapper() {
		return personInfoMapper;
	}

	public void setPersonInfoMapper(PersonInfoMapper personInfoMapper) {
		this.personInfoMapper = personInfoMapper;
	}

	public ProjectManagementMapper getProjectManagementMapper() {
		return projectManagementMapper;
	}

	public void setProjectManagementMapper(
			ProjectManagementMapper projectManagementMapper) {
		this.projectManagementMapper = projectManagementMapper;
	}

	public ProjectManagementExample getProjectmanagementExample() {
		return projectmanagementExample;
	}

	public void setProjectmanagementExample(
			ProjectManagementExample projectmanagementExample) {
		this.projectmanagementExample = projectmanagementExample;
	}

	public int countAllProject() {
		ProjectManagementExample example = new ProjectManagementExample();
		return this.projectManagementMapper.countByExample(example);		
	}

	/**
	 * 获取所有的项目
	 */
//	public List<ProjectManagement> getAllProject(Integer start, Integer end){
//		System.out.println(start);
//		System.out.println(end);
//		this.unitInfoMapper.selectByPrimaryKey(1);
//		return projectManagementMapper.getAllProject(start , end);
//	}
	
	@SuppressWarnings("unchecked")
	public List<ProjectDisplayDetail> getAllProject(Integer followup,Integer start, Integer end){

		List<ProjectManagement> pro = projectManagementMapper.getAllProject(followup , start , end);
		List<ProjectDisplayDetail> rs =new ArrayList();
		for(ProjectManagement tmp : pro){
			rs.add(this.getProjectDetail(tmp.getProjectid()));
		}
		return rs;
	}

	/**
	 * 插入项目记录
	 */
	public boolean insertProject(ProjectManagement projectManagement) {
		System.out.println("this is insertProject()");

		ProjectManagementExample example = new ProjectManagementExample();
		example.createCriteria().andProjectnumberEqualTo(projectManagement.getProjectnumber());
		if(this.projectManagementMapper.selectByExample(example).size()!=0||projectManagement.getProjectname()==null||projectManagement.getProjectnumber()==null){
			return false;
		}else{
			try{
			//插入项目相关的收费记录，验收记录并初始化。
				this.projectFeeMapper.insert(ProjectManagementToProjectFee.autoCreateFee(projectManagement));
				this.projectCheckRecordMapper.insert(ProjectManagementToProjectFee.autoCreateCheck(projectManagement));
				this.projectSuperVisoryManagementMapper.insert(ProjectManagementToProjectFee.autoCreateProjectSuperVisoryManagement(projectManagement));
				this.projectDistributeLogMapper.insert(ProjectManagementToProjectFee.autoCreateProjectDistributeLog(projectManagement));
			
				User user = ProjectManagementToProjectFee.autoCreateUser(projectManagement);
				if(projectManagement.getProjectdirector()!=null){
					user.setUsername(this.personInfoMapper.selectByPrimaryKey(projectManagement.getProjectdirector()).getName());
				}else{
					user.setUsername("项目负责人");
				}
				this.userMapper.insert(user);
				this.actualResultsMapper.insert(ProjectManagementToProjectFee.autoCreateActualResults(projectManagement));
			
				this.projectManagementMapper.insert(projectManagement);
				return true;
			}catch(Exception e){
				System.out.println(e);
				return false;
			}
		}		
	}

	/**
	 * 动态条件查询
	 */
	@SuppressWarnings("unchecked")
	public List<ProjectManagement> multiConditionQuery(String type,
			String keyWord, String beginYear, String endYear) {
		ProjectManagementExample example = new ProjectManagementExample();
		System.out.print(keyWord);
		if (!type.equals("") && type != null && !keyWord.equals("")
				&& keyWord != null)// 判断若查询条件且关键字都不为空，则嵌入该查询条件
		{
			if (type.equals("projectid")) {
				example.createCriteria().andProjectidEqualTo(
						Integer.valueOf(keyWord));
			}

			if (type.equals("projectnumber")) {
				example.createCriteria().andProjectnumberEqualTo(keyWord);
			}

			if (type.equals("projectname")) {
				example.createCriteria().andProjectnameEqualTo(keyWord);
			}

			if (type.equals("location")) {
				example.createCriteria().andLocationEqualTo(keyWord);
			}

			if (type.equals("mainundertakeunit")) {
				example.createCriteria().andMainundertakeunitEqualTo(
						Integer.valueOf(keyWord));
			}

			if (type.equals("guaranteeunit")) {
				example.createCriteria().andGuaranteeunitEqualTo(
						Integer.valueOf(keyWord));
			}

			if (type.equals("unitedunitone")) {
				example.createCriteria().andUnitedunitoneEqualTo(keyWord);
			}

			if (type.equals("unitedunitone")) {
				example.createCriteria().andUnitedunitoneEqualTo(keyWord);
			}

			if (type.equals("unitedunittwo")) {
				example.createCriteria().andUnitedunittwoEqualTo(keyWord);
			}

			if (type.equals("unitedunitthree")) {
				example.createCriteria().andUnitedunitthreeEqualTo(keyWord);
			}

			if (type.equals("unitedunitfour")) {
				example.createCriteria().andUnitedunitfourEqualTo(keyWord);
			}

			if (type.equals("establishprojectyear")) {
				example.createCriteria()
						.andEstablishprojectyearEqualTo(keyWord);
			}

			if (type.equals("mainmanagingdepartment")) {
				example.createCriteria().andMainmanagingdepartmentEqualTo(
						keyWord);
			}

			if (type.equals("followupperson")) {
				example.createCriteria().andFollowuppersonEqualTo(
						Integer.valueOf(keyWord));
			}

			if (type.equals("projectplancatagory")) {
				example.createCriteria().andProjectplancatagoryEqualTo(keyWord);
			}

			if (type.equals("field")) {
				example.createCriteria().andFieldEqualTo(keyWord);
			}

			if (type.equals("subfield")) {
				example.createCriteria().andSubfieldEqualTo(keyWord);
			}

			if (type.equals("issuefilenumber")) {
				example.createCriteria().andIssuefilenumberEqualTo(keyWord);
			}

			if (type.equals("winbidmoney")) {
				example.createCriteria().andWinbidmoneyEqualTo(
						Float.valueOf(keyWord));
			}

			if (type.equals("projectdirector")) {
				example.createCriteria().andProjectdirectorEqualTo(
						Integer.valueOf(keyWord));
			}

			if (type.equals("officephone")) {
				example.createCriteria().andOfficephoneEqualTo(keyWord);
			}

			if (type.equals("mobilephone")) {
				example.createCriteria().andMobilephoneEqualTo(keyWord);
			}

			if (type.equals("directoremail")) {
				example.createCriteria().andDirectoremailEqualTo(keyWord);
			}

			if (type.equals("contactman")) {
				example.createCriteria().andContactmanemailEqualTo(keyWord);
			}

			if (type.equals("phoneone")) {
				example.createCriteria().andPhoneoneEqualTo(keyWord);
			}

			if (type.equals("phonetwo")) {
				example.createCriteria().andPhonetwoEqualTo(keyWord);
			}

			if (type.equals("contactmanemail")) {
				example.createCriteria().andContactmanemailEqualTo(keyWord);
			}

			/*
			 * if(type.equals("checkdate")){
			 * example.createCriteria().andCheckdateEqualTo(null); }
			 */

			if (type.equals("projectschedule")) {
				example.createCriteria().andProjectscheduleEqualTo(keyWord);
			}

			if (type.equals("projectchange")) {
				example.createCriteria().andProjectchangeEqualTo(keyWord);
			}

			if (type.equals("inventpatentapplication")) {
				example.createCriteria().andInventpatentapplicationEqualTo(
						Integer.valueOf(keyWord));
			}

			if (type.equals("inventpatentauthorization")) {
				example.createCriteria().andInventpatentauthorizationEqualTo(
						Integer.valueOf(keyWord));
			}

			if (type.equals("usenewapplication")) {
				example.createCriteria().andUsenewapplicationEqualTo(
						Integer.valueOf(keyWord));
			}

			if (type.equals("usenewauthorization")) {
				example.createCriteria().andUsenewauthorizationEqualTo(
						Integer.valueOf(keyWord));
			}

			if (type.equals("appearancedesignapplication")) {
				example.createCriteria().andAppearancedesignapplicationEqualTo(
						Integer.valueOf(keyWord));
			}

			if (type.equals("appearancedesignauthorization")) {
				example.createCriteria()
						.andAppearancedesignauthorizationEqualTo(
								Integer.valueOf(keyWord));
			}

			if (type.equals("foreignpatent")) {
				example.createCriteria().andForeignpatentEqualTo(
						Integer.valueOf(keyWord));
			}

			if (type.equals("newtechnology")) {
				example.createCriteria().andNewtechnologyEqualTo(
						Integer.valueOf(keyWord));
			}

			if (type.equals("newproduct")) {
				example.createCriteria().andNewproductEqualTo(
						Integer.valueOf(keyWord));
			}

			if (type.equals("newmaterial")) {
				example.createCriteria().andNewmaterialEqualTo(
						Integer.valueOf(keyWord));
			}

			if (type.equals("newequipment")) {
				example.createCriteria().andNewequipmentEqualTo(
						Integer.valueOf(keyWord));
			}

			if (type.equals("computersoftware")) {
				example.createCriteria().andComputersoftwareEqualTo(
						Integer.valueOf(keyWord));
			}

			if (type.equals("paper")) {
				example.createCriteria().andPaperEqualTo(
						Integer.valueOf(keyWord));
			}

			if (type.equals("technologystandard")) {
				example.createCriteria().andTechnologystandardEqualTo(
						Integer.valueOf(keyWord));
			}

			if (type.equals("nationalaward")) {
				example.createCriteria().andNationalawardEqualTo(
						Integer.valueOf(keyWord));
			}

			if (type.equals("provincialaward")) {
				example.createCriteria().andProvincialawardEqualTo(
						Integer.valueOf(keyWord));
			}

			if (type.equals("cityaward")) {
				example.createCriteria().andCityawardEqualTo(
						Integer.valueOf(keyWord));
			}

			if (type.equals("doctor")) {
				example.createCriteria().andDoctorEqualTo(
						Integer.valueOf(keyWord));
			}

			if (type.equals("master")) {
				example.createCriteria().andMasterEqualTo(
						Integer.valueOf(keyWord));
			}

			if (type.equals("undergraduate")) {
				example.createCriteria().andUndergraduateEqualTo(
						Integer.valueOf(keyWord));
			}

			if (type.equals("fundsavailable")) {
				example.createCriteria().andFundsavailableEqualTo(
						Float.valueOf(keyWord));
			}

			if (type.equals("fundsinvestment")) {
				example.createCriteria().andFundsinvestmentEqualTo(
						Float.valueOf(keyWord));
			}

			if (type.equals("benefitacquired")) {
				example.createCriteria().andBenefitacquiredEqualTo(
						Float.valueOf(keyWord));
			}

			if (type.equals("projectsummary")) {
				example.createCriteria().andProjectsummaryEqualTo(keyWord);
			}
		}

		List<ProjectManagement> rs = new ArrayList();
		List<ProjectManagement> tmp = this.projectManagementMapper
				.selectByExample(example);
		if (beginYear != null && (!beginYear.equals(""))) {
			if (endYear != null && (!endYear.equals(""))) {
				for (ProjectManagement pro : tmp) {
					if (Integer.parseInt(pro.getEstablishprojectyear()
							.toString()) <= Integer.parseInt(endYear)
							&& Integer.parseInt(pro.getEstablishprojectyear()
									.toString()) >= Integer.parseInt(beginYear)) {
						rs.add(pro);
					}
				}// 以立项时间为查询条件之一
			} else {
				for (ProjectManagement pro : tmp) {
					if (Integer.parseInt(pro.getEstablishprojectyear()
							.toString()) >= Integer.parseInt(beginYear)) {
						rs.add(pro);
					}
				}
			}
		} else if (endYear != null && (!endYear.equals(""))) {
			for (ProjectManagement pro : tmp) {
				if (Integer.parseInt(pro.getEstablishprojectyear().toString()) <= Integer
						.parseInt(endYear)) {
					rs.add(pro);
				}

			}
		}
		return tmp;
	}
	public ProjectDisplayDetail getProjectDetail(Integer projectId){
		System.out.println(projectId);
		ProjectManagement project = this.projectManagementMapper.selectByPrimaryKey(projectId);
		UnitInfo u1 = this.unitInfoMapper.selectByPrimaryKey(project.getGuaranteeunit());
		UnitInfo u2 = this.unitInfoMapper.selectByPrimaryKey(project.getMainundertakeunit());
		PersonInfo p1 = this.personInfoMapper.selectByPrimaryKey(project.getContactman());
		PersonInfo p2 = this.personInfoMapper.selectByPrimaryKey(project.getProjectdirector());
		GeneralManager g = this.generalManagerMapper.selectByPrimaryKey(project.getFollowupperson());
		ProjectDisplayDetail tmp = new ProjectDisplayDetail();
		tmp.setAppearancedesignapplication(project.getAppearancedesignapplication());
		tmp.setAppearancedesignauthorization(project.getAppearancedesignauthorization());
		tmp.setBenefitacquired(project.getBenefitacquired());
		tmp.setCheckdate(project.getCheckdate());
		tmp.setCityaward(project.getCityaward());
		tmp.setComputersoftware(project.getComputersoftware());
		if(p1!=null&&p1.getName()!=null&&!(p1.getName().equals(""))){
			tmp.setContactman(p1.getName());
			tmp.setContactmanid(project.getContactman());
		}else{
			tmp.setContactman(null);
			tmp.setContactmanid(-1);
		}
		tmp.setContactmanemail(project.getContactmanemail());
		tmp.setContractbegin(project.getContractbegin());
		tmp.setContractend(project.getContractend());
		tmp.setDirectoremail(project.getDirectoremail());
		tmp.setDoctor(project.getDoctor());
		tmp.setEstablishprojectyear(project.getEstablishprojectyear());
		tmp.setField(project.getField());
		if(g!=null&&g.getGeneralmanagername()!=null&&!g.getGeneralmanagername().equals("")){
			tmp.setFollowupperson(g.getGeneralmanagername());
			tmp.setFollowuppersonid(project.getFollowupperson());
		}else{
			tmp.setFollowupperson(null);
			tmp.setFollowuppersonid(-1);
		}		
		tmp.setForeignpatent(project.getForeignpatent());
		tmp.setFundsavailable(project.getFundsavailable());
		tmp.setFundsinvestment(project.getFundsinvestment());
		if(u1!=null&&u1.getUnitname()!=null&&!u1.getUnitname().equals("")){
			tmp.setGuaranteeunit(u1.getUnitname());
			tmp.setGuaranteeunitid(project.getGuaranteeunit());
		}else{
			tmp.setGuaranteeunit(null);
			tmp.setGuaranteeunitid(-1);
		}
		
		tmp.setInventpatentapplication(project.getInventpatentapplication());
		tmp.setInventpatentauthorization(project.getInventpatentauthorization());
		tmp.setIssuefilenumber(project.getIssuefilenumber());
		tmp.setLocation(project.getLocation());
		tmp.setMainmanagingdepartment(project.getMainmanagingdepartment());
		if(u2!=null&&u2.getUnitname()!=null&&!u2.getUnitname().equals("")){
			tmp.setMainundertakeunit(u2.getUnitname());
			tmp.setMainundertakeunitid(project.getMainundertakeunit());
		}else{
			tmp.setMainundertakeunit(null);
			tmp.setMainundertakeunitid(-1);
		}
		
		tmp.setMaster(project.getMaster());
		tmp.setMobilephone(project.getMobilephone());
		tmp.setNationalaward(project.getNationalaward());
		tmp.setNewequipment(project.getNewequipment());
		tmp.setNewmaterial(project.getNewmaterial());
		tmp.setNewproduct(project.getNewproduct());
		tmp.setNewtechnology(project.getNewtechnology());
		tmp.setOfficephone(project.getOfficephone());
		tmp.setPaper(project.getPaper());
		tmp.setPhoneone(project.getPhoneone());
		tmp.setPhonetwo(project.getPhonetwo());
		tmp.setProjectchange(project.getProjectchange());
		if(p2!=null&&p2.getName()!=null&&!p2.getName().equals("")){
			tmp.setProjectdirector(p2.getName());
			tmp.setProjectdirectorid(project.getProjectdirector());
		}else{
			tmp.setProjectdirector(null);
			tmp.setProjectdirectorid(-1);
		}
		tmp.setProjectid(project.getProjectid());
		tmp.setProjectname(project.getProjectname());
		System.out.println();
		tmp.setProjectnumber(project.getProjectnumber());
		tmp.setProjectplancatagory(project.getProjectplancatagory());
		tmp.setProjectschedule(project.getProjectschedule());
		tmp.setProjectsummary(project.getProjectsummary());
		tmp.setProvincialaward(project.getProvincialaward());
		tmp.setSubfield(project.getSubfield());
		tmp.setTechnologystandard(project.getTechnologystandard());
		tmp.setUndergraduate(project.getUndergraduate());
		tmp.setUnitedunitfour(project.getUnitedunitfour());
		tmp.setUnitedunitone(project.getUnitedunitone());
		tmp.setUnitedunitthree(project.getUnitedunitthree());
		tmp.setUnitedunittwo(project.getUnitedunittwo());
		tmp.setUsenewapplication(project.getUsenewapplication());
		tmp.setUsenewauthorization(project.getUsenewauthorization());
		tmp.setWinbidmoney(project.getWinbidmoney());
		return tmp;
	}
	
	public void update(ProjectManagement projectManagement){
		this.projectManagementMapper.updateByPrimaryKey(projectManagement);
	}
	
	public void delete(Integer projectId){
		ProjectManagement projectManagement = this.projectManagementMapper.selectByPrimaryKey(projectId);
		
		ProjectFeeExample projectFeeExample = new ProjectFeeExample();
		projectFeeExample.createCriteria().andProjectnumberEqualTo(projectManagement.getProjectnumber());
		this.projectFeeMapper.deleteByExample(projectFeeExample);
		
		ProjectCheckRecordExample projectCheckRecordExample =new ProjectCheckRecordExample();
		projectCheckRecordExample.or().andProjectnumberEqualTo(projectManagement.getProjectnumber());
		this.projectCheckRecordMapper.deleteByExample(projectCheckRecordExample);
		
		ProjectSuperVisoryManagementExample projectSuperVisoryManagementExample = new ProjectSuperVisoryManagementExample();
		projectSuperVisoryManagementExample.or().andProjectnumberEqualTo(projectManagement.getProjectnumber());
		this.projectSuperVisoryManagementMapper.deleteByExample(projectSuperVisoryManagementExample);
		
		this.projectManagementMapper.deleteByPrimaryKey(projectId);
	}
	
	public ProjectManagement getById(Integer projectId){
		return this.projectManagementMapper.selectByPrimaryKey(projectId);
	}
	
	
	//测试用条件查询
	public List<ProjectDisplayDetail> conditionQuery(ProjectManagement projectManagement){
		List<ProjectManagement> pro=this.projectManagementMapper.conditionSelect(projectManagement);
		List<ProjectDisplayDetail> rs =new ArrayList();
		for(ProjectManagement tmp : pro){
			rs.add(this.getProjectDetail(tmp.getProjectid()));
		}
		return rs;		
	}

	public ProjectManagement getByNumber(String projectnumber) {
		// TODO Auto-generated method stub
		return this.projectManagementMapper.getByNumber(projectnumber);
	}

}
